<template>
    <div>

      <!--模态窗 多行-->
      <transition
        name="custom-classes-transition"
        enter-active-class="animated fadeInDown"
        leave-active-class="animated fadeOutUp"
      >

        <div class="modal-ctrl-body" v-show="addModalObj.isShow">
          <div class="modal-ctrl-main even-row-ctrl">
            <div class="modal-title">
              <p>新增用户</p>
            </div>
            <div class="modal-ctrl-content">

              <div class="even-row">
                <div class="modal-row">
                  <div class="modal-form-key f-l indispensable">
                    用户名：
                  </div>
                  <input type="text" maxlength="16" placeholder="最长16个字符" v-model="addUser.username" class="modal-text f-l">
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l indispensable ">
                    账号：
                  </div>
                  <input style="ime-mode:disabled" type="text" maxlength="16" placeholder="最长16个字符" v-model="addUser.loginName" class="modal-text f-l">
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l indispensable">
                    密码：
                  </div>
                  <input type="text" maxlength="16" placeholder="最长16位" v-model="addUser.pwd" class="modal-text f-l">
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l">
                    用户状态：
                  </div>
                  <select @change="selectUserState" :value="addUser.state">
                    <option value="1">启用</option>
                    <option value="0">禁用</option>
                  </select>
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l">
                    选择警员：
                  </div>
                  <select @change="selectPolice" @click="getStorage" v-model="selectPoliceId">
                    <option value="-1">不做选择</option>
                    <option v-for="item in PoliceHasNoUser" :value="item.policeId">
                      {{item.policeName}}
                      <!--— {{item.policeStationInfo.stationName}}-->
                    </option>
                  </select>
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l indispensable">
                    警员编号：
                  </div>
                  <input type="text" maxlength="16" placeholder="最长16个字符" v-model="addPolice.policeCode" class="modal-text f-l">
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l indispensable">
                    警员姓名：
                  </div>
                  <input type="text" maxlength="16" placeholder="最长16个字符" v-model="addPolice.name" class="modal-text f-l">
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l">
                    性别：
                  </div>
                  <select @change="selectGender" :value="addPolice.gender">
                    <option v-for="gender in genderArr" :value="gender.key">
                      {{gender.value}}
                    </option>
                  </select>
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l indispensable">
                    所属单位：
                  </div>
                  <input type="text"
                         readonly="readonly"
                         @click="sectionTreeAjaxFn"
                         v-model="sessionStorage.name" value="123" v-show="sessionStorage" class="modal-text f-l">
                  <input type="hidden" v-model="sessionStorage.id">
                  <div class="sectionTree" v-show="sectionTreeIShow">
                    <div id="sectionTree" class="ztree sectionTree-box"></div>
                  </div>
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l">
                    联系电话：
                  </div>
                  <input type="text" v-model="addPolice.telephone" class="modal-text f-l">
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l">
                    警员状态：
                  </div>
                  <select @change="selectStatus" :value="addPolice.status">
                    <option value="1">启用</option>
                    <option value="0">禁用</option>
                  </select>
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l indispensable">
                    警员类型：
                  </div>
                  <select @change="selectType" :value="addPolice.policeTypeId">
                      <option value="-1">请选择</option>
                      <option v-for="item in policeType" :value="item.id">
                        {{item.type}}
                      </option>
                  </select>
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l indispensable">
                    职位：
                  </div>
                  <select @change="selectPosition" :value="PositionValue">
                    <option value="-1">请选择</option>
                    <option v-for="(policeItem,index) in policePosition" :vlaue="policeItem.id" :key="index">
                      {{policeItem.position}}
                    </option>
                  </select>
                </div>
                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l">
                    办公电话：
                  </div>
                  <input type="text" v-model="addPolice.officeTel" class="modal-text f-l">
                </div>

                <div class="modal-row" v-show="addUserAndPolice">
                  <div class="modal-form-key f-l">
                    生日：
                  </div>
                  <div class="input-group date" id="date1">
                    <span class="input-group-addon glyphicon glyphicon-list"><i class="fa fa-calendar"></i></span>
                    <input type="hidden" ref="birthday" name="birthday">
                    <input type="text" readonly="readonly" ref="birthday" id="datetimepicker" class="form-control" required="required" data-date-format="dd-mm-yyyy">
                  </div>
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l">
                    排序：
                  </div>
                  <input type="text"  maxlength="4" placeholder="最长4位字符" v-model="addPolice.sort" class="modal-text f-l">
                </div>

                <div class="modal-row" v-if="addUserAndPolice">
                  <div class="modal-form-key f-l indispensable">
                    是否为领导：
                  </div>
                  <select @change="selectIsLeader" :value="addPolice.isLeader">
                    <option value="-1">请选择</option>
                    <option value="0">否</option>
                    <option value="1">是</option>
                  </select>
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l">
                    手台：
                  </div>
                  <input type="text" readonly="readonly" @click="broadcastingSearch('RADIO')" v-model="broadcastingSelectSub.RADIO.num" class="modal-text f-l">
                  <input type="button" v-show="broadcastingSelectSub.RADIO.num" @click="clearBroadcasting('RADIO')" class="clear-select-btn" value="清除选择">
                  <!--<input type="hidden" v-model="broadcastingSelectSub.radio.id">-->
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l">
                    警务通：
                  </div>
                  <input type="text" readonly="readonly" @click="broadcastingSearch('appAccount')" v-model="broadcastingSelectSub.appAccount.num" class="modal-text f-l">
                  <input type="button" v-show="broadcastingSelectSub.appAccount.num" @click="clearBroadcasting('appAccount')" class="clear-select-btn" value="清除选择">
                  <!--<input type="hidden" v-model="broadcastingSelectSub.Jwt.id">-->
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l">
                    警车：
                  </div>
                  <input type="text" readonly="readonly" @click="broadcastingSearch('CAR')" v-model="broadcastingSelectSub.CAR.carcode" class="modal-text f-l">
                  <input type="button" v-show="broadcastingSelectSub.CAR.carcode" @click="clearBroadcasting('CAR')" class="clear-select-btn" value="清除选择">
                  <!--<input type="hidden" v-model="broadcastingSelectSub.CAR.id">-->
                </div>

                <div class="modal-row">
                  <div class="modal-form-key f-l">
                    单兵：
                  </div>
                  <input type="text" readonly="readonly" @click="broadcastingSearch('MONITOR')" v-model="broadcastingSelectSub.MONITOR.num" class="modal-text f-l">
                  <input type="button" v-show="broadcastingSelectSub.MONITOR.num" @click="clearBroadcasting('MONITOR')" class="clear-select-btn" value="清除选择">
                </div>

                <div class="modal-row photo" v-show="addUserAndPolice" style="display: block">
                  <div class="modal-form-key f-l">
                    警员相片：
                  </div>
                  <div class="photo-up-btn f-l">
                    <input type="file" name="photo" ref="formFile" @change="filePhoto" class="modal-text f-l">
                    <span class="photo-btn">选择相片</span>
                    <input type="hidden" name="photoId" v-model="addPolice.pictureUrl">
                  </div>
                  <button type="button" :disabled="subImgDls" class="btn btn-info f-l" @click="bindPhoto" v-show="photoSrc!==''">上传</button>
                  <button type="button" class="btn btn-info f-l" @click="clearPhoto" v-show="photoSrc!==''">清除</button>
                  <img :src="photoSrc" class="f-l photo-img" alt="">
                </div>

              </div>
            </div>
            <div class="btns text-r">
               <button type="button" class="btn btn-default" @click="closeModal">取消</button>
              <button type="button" class="btn btn-info" @click="subClick">确定</button>
            </div>
          </div>
        </div>
      </transition>


      <!--绑定设备-->
      <transition
        name="custom-classes-transition"
        enter-active-class="animated fadeInDown"
        leave-active-class="animated fadeOutUp"
      >
        <div class="modal-ctrl-body" v-show="equipmentModalIsShow">
          <div class="modal-ctrl-main even-row-ctrl">
            <div class="modal-title">
              <p>设备选择</p>
            </div>

            <div class="modal-ctrl-content" v-show="false">
              <div class="search-ctrl f-l" >
                <form style="height: 100%;">
                  <input placeholder="搜索关键字" class="search-text">
                  <button class="search-btn btn btn-info" type="button">搜索</button>
                </form>
              </div>
            </div>

            <div class="table-ctrl">
              <!--表格头-->
              <div class="table-header">
                <table class="table-default">
                  <thead>
                  <tr>
                    <th v-for="ths in policeFacilityArr.tHeader">
                      {{ths.value}}
                    </th>
                  </tr>
                  </thead>
                </table>
              </div>

              <!--表格中部-->
              <div class="table-overflowX" style="height: 300px;">
                <table class="table-default">
                  <tbody>
                  <tr
                    v-for="(trs,index) in policeFacilityArr.tBody"
                    :class="{active: index === isActive}"
                    @click="selectBroadcasting(trs,index)">
                    <td v-for="tds in policeFacilityArr.tHeader">
                      {{trs[tds.key]}}
                      <!--{{trs.carcode}}-->
                    </td>
                  </tr>
                  </tbody>
                </table>
              </div>

            </div>

            <!--表格翻页  -->
            <Pages
              v-show="policeFacilityArr.pages !== 0"
              :showItem="10"
              :pageSize="20"
              :allDataSum="policeFacilityArr.pages"
              :allpage="(Math.ceil(policeFacilityArr.pages/20))"
              @current-change="tablePageData"
            ></Pages>

            <div class="btns text-r">
              <button type="button" class="btn btn-default" @click="modalClose">取消</button>
              <button type="button" class="btn btn-info" @click="broadcastingSelectBtn">确定</button>
            </div>
          </div>
        </div>
      </transition>

    </div>
</template>

<script>
  import Pages from "../../sudoku/atom/pages.vue"
    export default {
        name: "Modal",
        props:["stationData","addModalObj","PoliceHasNoUser"],
        components:{
          Pages
        },
        data(){
          return {
            isActive:-1,
            setting : {
              data: {
                simpleData: {
                  enable: true
                },
              }
            },
            addUserAndPolice:true,
            addUser:{ //添加用户
              username:"",
              loginName:"",
              pwd:"",
              state:"1"
            },
            addPolice:{ //添加警员
              policeCode:"", //警员code
              name:"", //名称
              telephone:"", //联系电话
              officeTel:"", //办公电话
              gender:"男",
              status:"1", //状态
              policeTypeId:"-1", //警员类型
              policePosition:"-1", //警员职位
              birthday:"",
              sort:"",
              isLeader:"-1", //是否领导
              pictureUrl:"" //相片Id
            },
            sectionName:"",
            policeType:[],
            policePosition:[],
            sessionStorage:{},
            sectionTreeIShow:false,
            treeNodes:[],
            photoSrc:"", //图片地址
            photoFile:"", //图片文件
            equipmentModalIsShow:false, //设备弹窗
            policeFacilityArr:{//设备管理渲染
              tHeader:[],
              tBody:[],
              pages:0
            },
            broadcastingSelect:{ //设备选择
              obj:null,
              name:"",
              id:""
            },
            subImgDls:false,
            brodcastingType:"",
            broadcastingSelectSub:{//设备提交对象
              RADIO:{ //手台
                num:"",
                id:""
              },
              appAccount:{ //警务通
                id:"",
                num:""
              },
              CAR:{ //警车
                id:"",
                carcode:""
              },
              MONITOR:{ //单兵
                id:"",
                num:""
              }
            },
            clearPageIndex:0, //分页清除
            addSubData:"",
            selectPoliceId:"-1", //不选择警员
            statusArr:[], //警员状态
            genderArr:[ //性别
              {"key":"男","value":"男"},
              {"key":"女","value":"女"}
            ],
            PositionValue:"-1"//
          }

        },
        watch:{
          addModalObj:{
            handler(v1){
              if( v1.isShow === true ){ //监听弹窗变化。
                let session = JSON.parse(sessionStorage.getItem("stationCommunication"));
                this.sessionStorage.name = session.name;
                this.sessionStorage.id = session.id;
              }
            },
            deep:true
          }
        },
        methods:{
          //关闭弹窗
          closeModal(){
            this.$emit("closeModal");
            this.initUserData();// 清除用户缓存
            this.initPoliceData();//清除警员缓存
            this.initbroadcastingData(); //清除设备绑定缓存
            this.selectPoliceId = "-1";//清除选择警员
            this.PositionValue = "-1";//清除职位缓存
            //console.log(this.selectPoliceId);
          },
          //选择警员
          selectPolice(ele){
            this.selectPoliceId = ele.target.value;
            //console.log(ele.target.value);
            if( this.selectPoliceId === "-1" ){
              this.addUserAndPolice = true;
            }else{ //新增用户
              this.addUserAndPolice = false;
            }
          },
          //选择用户状态
          selectUserState(ele){
            this.addUser.state = ele.target.value;
          },
          //是否为领导
          selectIsLeader(ele){
            this.addPolice.isLeader = ele.target.value;
          },
          //选择性别
          selectGender(ele){
            this.addPolice.gender = ele.target.value;
          },
          //警员相片
          filePhoto(ele){
            this.photoFile = ele.target.files[0];
            if( this.photoFile.type ){
              let imgTypeArr = this.photoFile.type.split("/");
              let imgType = imgTypeArr[imgTypeArr.length-1];
              //console.log(imgType);
              if( this.verifyForm.isImg(imgType) ){
                this.photoSrc = window.URL.createObjectURL(this.photoFile);
                this.subImgDls = false;
              }else{
                this.s_alert("选择的不是图片文件！");
                this.photoSrc = "";
                this.$refs.formFile.value = "";
              }
            }else{
              this.s_alert("选择的不是图片文件！");
            }
          },
          //提交
          subClick(){

            let userParams = {
              pwd :this.addUser.pwd ,
              state :this.addUser.state ,
              username  :this.addUser.username,
              loginName  :this.addUser.loginName,
            };

            ////console.log( userParams.loginName.replace(/[/u0391-/uFFE5]/gi) )
         /*   for(let i=0;i<this.addUser.loginName.length;i++){
              if(this.addUser.loginName.charCodeAt(i)>19968&&this.addUser.loginName.charCodeAt(i)<40869){
                this.s_alert("账号不能为汉字！");
                // alert("账号不能为汉字！");
                return false;
              }
            }*/
            if( this.verifyForm.isNull(userParams.loginName) || this.verifyForm.letterAndNumber(userParams.loginName) ){
              this.s_alert("账号为空或格式不正确！只能为字母和数字！");
              // alert("用户名为空！");
              return false;
            }else if( this.verifyForm.isNull(userParams.pwd) || this.verifyForm.letterAndNumber(userParams.pwd) ){
              this.s_alert("密码为空或格式不正确！只能为字母和数字！");
              // alert("密码为空！");
              return false;
            }else if( this.verifyForm.isNull(userParams.username) ){
              this.s_alert("用户名为空！");
              // alert("用户名为空！");
              return false;
            }
          /*  else if( this.verifyForm.isNull(this.addUser.loginName) ){
              this.s_alert("账号为空！");
              // alert("账号为空！");
              return false;
            }*/
            if( this.selectPoliceId === "-1" ){//新增用户和警员
              userParams.loginname = this.addUser.loginName;
              let policeParams = {
                status: this.addPolice.status,
                policeTypeId: this.addPolice.policeTypeId,
                policePosition: this.addPolice.policePosition,
                officeTel: this.addPolice.officeTel,
                sort: this.addPolice.sort,
                gender: this.addPolice.gender,
                stationId:this.sessionStorage.id,
                telephone:this.addPolice.telephone,
                birthday:this.$refs.birthday.value,
                name:this.addPolice.name,
                policeCode:this.addPolice.policeCode,
                pictureUrl  :this.addPolice.pictureUrl,
                isLeader  :this.addPolice.isLeader,
              };
              //合并用户和警员传递对象
              for( let policeKey in policeParams ){
                for( let userKey in userParams ){
                  if( policeKey !== userKey ){
                    policeParams[userKey] = userParams[userKey]
                  }
                }
              }
              if( this.verifyForm.isNull(policeParams.policePosition) ){
                this.s_alert("请选择警员职位！");
                // alert("请选择警员职位！");
                return false;
              }else if( this.verifyForm.isNull(policeParams.policeTypeId) ){
                this.s_alert("请选择警员类型！");
                // alert("请选择警员类型！");
                return false;
              }else if( this.verifyForm.isNull(policeParams.name) ){
                this.s_alert("请输入警员姓名！");
                // alert("请输入警员姓名！");
                return false;
              }else if( this.verifyForm.isNull(policeParams.policeCode) || this.verifyForm.letterAndNumber(policeParams.policeCode) ){
                this.s_alert("警员编号为空或格式不正确！只能输入字母和数字！");
                // alert("请输入警员编号！");
                return false;
              }else if( this.verifyForm.isNull(policeParams.stationId) ){
                this.s_alert("请选择警员部门！");
                // alert("请选择警员部门！");
                return false;
              }else if( this.verifyForm.isNull(policeParams.loginname) ){
                this.s_alert("账号不能为空！");
                // alert("账号不能为空！");
                return false;
              }else if( this.verifyForm.iphone(policeParams.telephone) ){
                this.s_alert("联系电话格式不正确！");
                // alert("联系电话格式不正确！");
                return false;
              }else if( this.verifyForm.isIphoneOrTell(policeParams.officeTel) ){
                this.s_alert("办公电话格式不正确！");
                // alert("办公电话格式不正确！");
                return false;
              }else if( this.verifyForm.isNull(policeParams.isLeader) ){
                this.s_alert("请选择是否为领导！");
                // alert("请选择是否为领导！");
                return false;
              }else if( this.verifyForm.isNumber(policeParams.sort) ){
                this.s_alert("排序只能为正整数！");
                // alert("请选择是否为领导！");
                return false;
              }
              //console.log(policeParams);

              policeParams.birthday ?  policeParams.birthday = policeParams.birthday+"T02:09:17.245+05:30":policeParams.birthday = "";
              this.$axios.post(this.HOST + "/user/insertPoliceInfoAndUser",policeParams)
                .then((data)=>{
                  //console.log(data);
                  if( data.data.success === true || data.data.success === "true" ){
                    this.s_alert("新增成功");
                    // alert("新增成功");
                    const userId = data.data.data.id;
                    this.facilitySubFn(userId); //设备绑定
                    this.$emit("addUserSucceed"); //新增成功，通知刷新
                    this.s_logwrite("基础信息管理-用户管理","新增用户与警员");
                    this.initUserData();// 清除用户缓存
                    this.initPoliceData();//清除警员缓存
                    this.PositionValue = "-1";//清除职位缓存
                  }else{
                    let msg = data.data.errorMsg.split('[');
                    this.s_alert(msg[0]);
                    // alert(msg[0]);
                  }
                })
                .catch((err)=>{
                  //console.log(err);
                });
            }else { //新增用户
              userParams.policeGuid = this.selectPoliceId; //警员id
              userParams.loginName = this.addUser.loginName; //用户名
              //console.log(userParams);
              this.$axios.post(this.HOST + "/user/addUserInfo",userParams)
                .then((data)=>{
                  //console.log(data);
                  if( data.data.success === true || data.data.success === "true" ){
                    this.s_alert("新增成功");
                    // alert("新增成功");
                    const userId = data.data.data.id;
                    this.facilitySubFn(userId);//设备绑定
                    this.selectPoliceId = "-1";
                    this.s_logwrite("基础信息管理-用户管理","新增用户");
                    this.initUserData(); //清楚用户缓存
                    this.initPoliceData();//清除警员缓存
                    this.$emit("addUserSucceed"); //新增成功，通知刷新
                  }else {
                    let msg = data.data.errorMsg.split("[");
                    this.s_alert(msg[0]);
                    // alert(msg[0]);
                  }
                })
                .catch((err)=>{
                  //console.log(err);
                });
            }
          },
          //清除相片
          clearPhoto(){
            this.photoSrc = "";
            this.addPolice.pictureUrl = "";
            this.$refs.formFile.value = "";
          },
          //绑定相片
          bindPhoto(userId){
            //jp-TIFS-FileCenter-ms/file?businessId=
            this.subImgDls = true;
            let param = new FormData();
            param.append('file',this.photoFile);//通过append向form对象添加数据
            //console.log(this.photoFile);
            let configFile = {
              headers:{'Content-Type':'multipart/form-data'}
            };
            this.$axios.post(baseURL+"api/jp-TIFS-FileCenter-ms/file",param,configFile)
              .then((data)=>{
                this.s_alert("上传成功！");
                // alert("上传成功！");
                this.s_logwrite("基础信息管理-用户管理","给警员设置了相片");
                //console.log(data.data.fileID);
                this.addPolice.pictureUrl = data.data.fileID;
              })
              .catch((err)=>{
                this.s_alert("上传失败！");
                // alert("上传失败！");
                //console.log(err);
              });
          },
          //警员类型选择
          selectType(ele){
            //console.log(ele);
            this.addPolice.policeTypeId = ele.target.selectedOptions[0].value;
            //console.log(this.addPolice);
          },
          //警员职位选择
          selectPosition(ele){
            //console.log(ele);
            this.addPolice.policePosition = ele.target.selectedOptions[0].attributes[1].value; //传递属性值
            this.PositionValue = ele.target.selectedOptions[0].value; //显示属性名
            //console.log(this.addPolice);
          },
          //警员状态选择
          selectStatus(ele){
            //console.log(ele.target.selectedOptions[0].value);
            this.addPolice.status = ele.target.selectedOptions[0].value;
            //console.log(this.addPolice);
          },
          //设备绑定提交
          facilitySubFn(userID){
            //创建用户成功关联
            let valueParams = [];
            //console.log(this.broadcastingSelectSub);
            for( let key in this.broadcastingSelectSub ){
              if( this.broadcastingSelectSub[key].num || this.broadcastingSelectSub[key].carcode ){
                let itemArr = ["POLICE",userID];
                itemArr.push(key);
             /*
                原警员物资绑定方法
                let _sjon = {
                  "resourceEntryCode":"POLICE",
                  "resourceId":userID,
                  "targetEntryCode":key.toUpperCase()
                };
                if( this.broadcastingSelectSub[key].carcode ){
                  _sjon.targetId = this.broadcastingSelectSub[key].carcode;
                }else if( this.broadcastingSelectSub[key].num ){
                  _sjon.targetId = this.broadcastingSelectSub[key].num;
                }
                facilityParams.push(_sjon);*/
                if( this.broadcastingSelectSub[key].carcode ){
                  itemArr.push(this.broadcastingSelectSub[key].carcode)
                }else if( this.broadcastingSelectSub[key].num ){
                  itemArr.push(this.broadcastingSelectSub[key].num)
                }
                valueParams.push(itemArr);
              }
            }

            //POST /base/save
            let baseSaveParams = {
              "columns":["resource_Entry_Code","resource_Id","target_Entry_Code","target_Id"],
              "dataTypeCode": "calendarext",
              "datasList": valueParams,
              "tableName": "T_DA_RESOURCE_RELATION"
            };
            //console.log(baseSaveParams);
            if( valueParams.length>0 ){
              // this.policeFacility+"/api/jp-DA-CalendarExt-business-ms/facilityRelation/static/saveRelationship"
              this.$axios.post(this.HOST+"/base/save/batch",baseSaveParams)
                .then((data)=>{
                  this.s_alert("设备绑定成功");
                  // alert("设备绑定成功");
                  this.s_logwrite("基础信息管理-用户管理","给用户绑定了设备");
                  //清除设备缓存
                  this.initbroadcastingData();
                  //console.log(data);
                })
                .catch((err)=>{
                  //console.log(err);
                })
            }
          },
          //警员类型列表
          policeTypeFn(){
            this.$axios.get(this.HOST+"/police/getAllPoliceType")
              .then((data)=>{
                this.policeType = data.data;
                //console.log(this.policeType);
              })
              .catch((err)=>{
                //console.log(err);
              })
          },
          //警员岗位列表
          policePositionFn(){
            this.$axios.get(this.HOST+"/police/getAllPolicePosition")
              .then((data)=>{
                this.policePosition = data.data;
                //console.log(this.policePosition);
              })
              .catch((err)=>{
                //console.log(err);
              })
          },
          //新增弹窗树结构
          sectionTreeAjaxFn(){
            //alert(1);
            this.sectionTreeIShow = !this.sectionTreeIShow;
            if( this.sectionTreeIShow ){
              this.$axios.get(this.HOST+"/station/getOrgaInfo")
                .then((data)=>{
                  ////console.log(data);
                  const _this = this;
                  this.setting.callback = {
                    onClick:function(event,treeId,treeNode){
                      //console.log(treeNode);
                      _this.sessionStorage.id = treeNode.id;
                      _this.sessionStorage.name = treeNode.name;
                      _this.sectionTreeIShow = false;
                    }
                  };
                  _this.treeNodes = data.data.data;
                  $.fn.zTree.init($("#sectionTree"), this.setting, _this.treeNodes);
                })
                .catch((err)=>{
                  //console.log(err);
                })
            }
          },
          //清除选择
          clearBroadcasting(type){
            if( type === 'CAR' ){
              this.broadcastingSelectSub[type].carcode = "";
            }else{
              this.broadcastingSelectSub[type].num = "";
            }
          },
          //设备查询
          broadcastingSearch(type,pageIndex){
            this.equipmentModalIsShow = true;
            this.brodcastingType = type;
            //获取缓存信息 部门id
            //this.getStorage();
            //console.log(this.sessionStorage.id);
            if( type === "RADIO" ){ //手台
              this.policeFacilityArr.tHeader = [
                {"key":"isWork","value":"工作状态"},
                {"key":"num","value":"手台编号"},
                {"key":"state","value":"在线状态"},
                {"key":"type","value":"手台类型"},
                {"key":"stationName","value":"所属单位"},
              ];
              let params = {
                stationId: this.sessionStorage.id,
                pageSize: 20
              };
              pageIndex ? params.pageIndex = pageIndex : params.pageIndex = "0";
              this.broadcastingAjax(this.policeFacility+"/api/birm/policefacility/radio/getByStationId",params,"RADIO");
            }else if( type === "appAccount" ){ //警务通
              this.policeFacilityArr.tHeader = [
                {"key":"isWork","value":"工作状态"},
                {"key":"num","value":"设备编号"},
                {"key":"status","value":"在线状态"},
              ];
              let params = {
                stationId: this.sessionStorage.id,
                pageSize: 20
              };
              pageIndex ? params.pageIndex = pageIndex : params.pageIndex = "0";
              this.broadcastingAjax(this.policeFacility+"/api/birm/policefacility/jwt/findByStationId",params,"appAccount");
            }else if( type === "CAR" ){ //警车
              this.policeFacilityArr.tHeader = [
                {"key":"carcode","value":"警车车牌"},
                {"key":"isonline","value":"是否在线"},
                {"key":"carType","value":"车辆类型"}
              ];
              let params = {
                stationId: this.sessionStorage.id,
                pageSize: 20
              };
              pageIndex ? params.pageIndex = pageIndex : params.pageIndex = "0";
              this.broadcastingAjax(this.policeFacility+"/api/birm/policeFacility/carInfos/querycars",params,"CAR");
            }else if( type === "MONITOR" ){ //单兵
              this.policeFacilityArr.tHeader = [
                {"key":"name","value":"设备名称"},
                {"key":"tel","value":"手机号"},
                {"key":"equipnum","value":"设备号"},
              ];
              let params = {
                stationId: this.sessionStorage.id,
                pageSize: 20
              };
              pageIndex ? params.pageIndex = pageIndex : params.pageIndex = "0";
              this.broadcastingAjax(this.policeFacility+"/api/birm/policefacility/danbing/getDanBingByStationId",params,"MONITOR");
            }
          },
          //设备渲染
          broadcastingAjax(url,par,type){
            this.$axios.get(url,{
              params:par
            })
              .then((data)=>{
                this.policeFacilityArr.pages = data.data.elementsSum;
                if( type === "RADIO" ){
                  for( let i=0;i<data.data.data.length;i++ ){
                    data.data.data[i].isWork = data.data.data[i].isWork === 1 ?  "正常" : "损坏";
                    data.data.data[i].state = data.data.data[i].state === 1 ?  "在线" : "离线";
                    //console.log(data.data.data[i].type);
                    switch (data.data.data[i].type){
                      case 0:
                        data.data.data[i].type = "手台";
                        break;
                      case 1:
                        data.data.data[i].type = "车台";
                        break;
                      case 2:
                        data.data.data[i].type = "基地台";
                        break;
                    }
                  }
                  ////console.log(data.data.data);
                }else  if( type === "appAccount" ){
                  for( let i=0;i<data.data.data.length;i++ ){
                    data.data.data[i].isWork = data.data.data[i].isWork == "1" ?  "正常" : "损坏";
                    data.data.data[i].status = data.data.data[i].status == "1" ?  "在线" : "离线";
                  }
                  ////console.log(data.data.data);
                }else  if( type === "CAR" ){
                  for( let i=0;i<data.data.data.length;i++ ){
                    //data.data.data[i].isWork = data.data.data[i].isWork = "1" ?  "正常" : "损坏";
                    data.data.data[i].isonline = data.data.data[i].isonline == "1" ?  "在线" : "离线";
                    data.data.data[i].carType = data.data.data[i].carType ? data.data.data[i].carType.type : "";
                  }
                  ////console.log(data.data.data);
                }else  if( type === "MONITOR" ){
                  for( let i=0;i<data.data.data.length;i++ ){
                    data.data.data[i].isWork = data.data.data[i].isWork == "1" ?  "正常" : "损坏";
                    data.data.data[i].status = data.data.data[i].status == "1" ?  "在线" : "离线";
                  }
                  ////console.log(data.data.data);
                }

                this.policeFacilityArr.tBody = data.data.data;

                //console.log(data);
              })
              .catch((err)=>{
                //console.log(err);
              })
          },
          //设备选择
          selectBroadcasting(broadcastingObj,index){
            //console.log(broadcastingObj);
            this.isActive = index;
            this.broadcastingSelect.obj = broadcastingObj;
          },
          //设备分页
          tablePageData(page){
            page--;
            this.broadcastingSearch(this.brodcastingType,page);
          },
          //设备选择确定
          broadcastingSelectBtn(){
            //console.log(this.broadcastingSelect);
            const obj = this.broadcastingSelect.obj;
            //console.log(this.brodcastingType);
            if( obj ){//设备对象存在
              //console.log(obj);
              if( this.brodcastingType === "RADIO" ){ //手台
                this.broadcastingSelectSub.RADIO.num = obj.num;
                this.broadcastingSelectSub.RADIO.id = obj.id;
              }else if( this.brodcastingType === "appAccount" ){ //警务通
                this.broadcastingSelectSub.appAccount.num = obj.num;
                this.broadcastingSelectSub.appAccount.id = obj.id;
              }else if( this.brodcastingType === "CAR" ){ //车辆
                this.broadcastingSelectSub.CAR.id = obj.id;
                this.broadcastingSelectSub.CAR.carcode = obj.carcode;
              }else if( this.brodcastingType === "MONITOR" ){ //单兵
                this.broadcastingSelectSub.MONITOR.num = obj.equipnum;
                this.broadcastingSelectSub.MONITOR.id = obj.id;
              }
            }
            //console.log(this.broadcastingSelectSub);
            this.modalClose();
          },
          //窗口关闭
          modalClose(){
            this.equipmentModalIsShow = !this.equipmentModalIsShow;
            this.isActive = "";
            this.broadcastingSelect= { //设备选择
              obj:null,
              name:"",
              id:""
            }
          },
          //初始化缓存信息
          getStorage(){
            //console.log(sessionStorage);
            let getSessionStorage = JSON.parse(sessionStorage.getItem("stationCommunication"));
            this.sessionStorage = {
              id:getSessionStorage.id,
              name:getSessionStorage.name
            }
            //console.log(this.sessionStorage);
          },
          //初始化新增用户信息
          initUserData(){
            this.addUser.username ="";
            this.addUser.loginName ="";
            this.addUser.pwd = "";
            this.addUser.state = "1";
            this.addUserAndPolice = true;
          },
          //初始化新增警员信息
          initPoliceData(){
            this.addPolice.policeCode ="";
            this.addPolice.name ="";
            this.addPolice.telephone = "";
            this.addPolice.officeTel = "";
            this.addPolice.gender = "男";
            this.addPolice.status = "1";
            this.addPolice.policeTypeId = "-1";
            this.addPolice.policePosition = "-1";
            this.addPolice.birthday = "";
            this.addPolice.sort = "";
            this.addPolice.pictureUrl = "";
            this.addPolice.isLeader = "-1";
            this.$refs.formFile.value = "";
            this.$refs.birthday.value = "";
            this.photoSrc = "";
            this.subImgDls = false;
          },
          //初始化设备绑定
          initbroadcastingData(){
            this.broadcastingSelectSub = {//设备提交对象
              RADIO:{ //手台
                num:"",
                id:""
              },
              appAccount:{ //警务通
                id:"",
                num:""
              },
              CAR:{ //警车
                id:"",
                carcode:""
              },
              MONITOR:{ //单兵
                id:"",
                num:""
              }
            }
          }
        },
        created() {
          let _this = this;
          //console.log(this.addModalObj);
          //日期控件
          $(function () {
            //console.log($("#datetimepicker"));
            $('#date1').datetimepicker({
              language: "zh-CN",
              format: "yyyy-mm-dd",
              minView: "month",
              autoclose: true
            }).on("changeDate",function(ev){
              ////console.log(ev.date.valueOf());
              let selectDate = ev.date.valueOf();
              let theDate = new Date().getTime(); //当前时间
              if( selectDate >= theDate ){
                _this.s_alert("选择时间不能是未来时间！");
                // alert("选择时间不能是未来时间！");
                $(".form-control").val("");
                $(".form-control").prev().val("");
              }
            });
          });

          //所有警员类型渲染
          this.policeTypeFn();
          //所有警员岗位渲染
          this.policePositionFn();
          //弹出窗口所有树
          //this.sectionTreeAjaxFn();
          //this.broadcastingSearch();

        }

    }
</script>

<style scoped>
  .fadeInDown,.fadeOutUp{
    animation-duration: .500s;
  }
  .form-control{
    height: 100%;
    border: 1px solid #ccc;
  }
  .sectionTree-box{
    position: absolute;
    z-index: 99;
    padding: 10px;
    right: 0;
    width: 60%;
    top: 30px;
    height: 200px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid rgba(51,51,51,0.2);
  }

  .modal-ctrl-content:after{
    content: " ";
    display: block;
    height: 0;
    visibility: hidden;
    clear: both;
  }
</style>
